package code3;

/**
 * @author noob
 * @version 1.0
 * @date 2021/3/17 10:26
 * 查找小于target的最大值  ----有bug
 */
public class BinarySearchLower_upperfloor {







    public static <E extends Comparable<E>> int upperlower(E[] data,E target){

        int left = -1;
        int right = data.length-1;

        while (left < right){
            int mid = left + (right - left+1)/2;
            //在data[mid]==target的时候，有可能是解，所以不能排除，需要放到循环不变量中去
            if (data[mid].compareTo(target) <=0){
                //去右边找
                left = mid;
            }else{
                right = mid - 1;
            }
        }
        return left;


    }



    public static void main(String[] args) {
        Integer[] arr = {1,1,3,3,5,5};
        for (int i = 0; i <= 6 ; i++) {
            int search = BinarySearchLower_upperfloor.upperlower(arr, i);
            System.out.print(search + " ");
        }
        System.out.println();
    }
}
